package com.hao.hospital.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.terracotta.statistics.Time;

import com.hao.hospital.model.HospitalPerson;
import com.hao.hospital.model.MedicalRecord;
import com.hao.hospital.model.Person;
import com.hao.hospital.model.RegisterRecord;
import com.hao.hospital.model.User;
import com.hao.hospital.util.GeneralMethod;
import com.hao.hospital.util.TimeUtil;
import com.jfinal.aop.Enhancer;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import cn.jlook.jfinal.ext3.kit.SqlBuilder;
import cn.jlook.jfinal.ext3.kit.SqlBuilder.JOIN;

public class MedicalService extends AbstractService {
	private static MedicalService me;

	public static MedicalService me() {
		if (null == me) {
			me = Enhancer.enhance(MedicalService.class);
		}
		return me;
	}
	/**
	 * 获取所有患者预约记录
	 * @param pageNumber
	 * @return
	 */
	public Page<Record> getAllUserRegister(int pageNumber){
		Page<Record> page=SqlBuilder.from(RegisterRecord.table)
				.alias("r").field("*")
				.join(JOIN.LEFT,User.table, "u", "r.user_id=u.user_id")
				.join(JOIN.LEFT, HospitalPerson.table, "h", "r.person_id=h.person_id")
				.order("r.worktime,r.create_time,u.user_name,r.flag")
				.page(pageNumber, 8);
		List<Record> rs=page.getList();
		for(Record r:rs){
			r.set("CNcreatetime",TimeUtil.me().stampToDateInYear(r.getLong("create_time").toString()));
			r.set("CNworktime", TimeUtil.me().stampToDateInYear(r.getLong("worktime").toString()));
			r.set("CNflag", GeneralMethod.isFlag(r.getInt("flag")));
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex"))
					);
		}
		return page;
	}
	
	public Page<Record> getInfoInput(int person_id, int pageNum,String condition){
		if(StringUtils.isEmpty(condition)){
			condition="'%'";
		}else{
			condition="'%"+condition+"%'";
		}
		Page<Record> page = SqlBuilder.from(MedicalRecord.table).alias("m")
				.join(JOIN.LEFT, User.table, "u", "m.user_id=u.user_id").field("*").where("person_id=? and (u.user_name like "+condition+" or u.user_id like "+condition+")")
				.order("create_time desc").page(pageNum, 8, person_id);
		List<Record> records = page.getList();
		for (Record r : records) {
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex")));
			r.set("CNcreatetime", TimeUtil.me().stampToDateInYear(r.getLong("create_time").toString()));
			r.set("action", "userPage");
		}
		return page;
	}
	/**
	 * 通过医生id获取医生所对应的患者的就诊记录,按创建日期排序 从大到小
	 * 
	 * @return
	 */
	public Page<Record> getInfoByPersonId(int person_id, int pageNum,String...condition) {
		String c="";
		if(StringUtils.isEmpty(condition[0])){
			c="'%'";
			condition[0]="%20";
		}else{
			c="'%"+condition[0]+"%'";
		}
		Page<Record> page = SqlBuilder.from(MedicalRecord.table).alias("m")
				.join(JOIN.LEFT, User.table, "u", "m.user_id=u.user_id").field("*").where("person_id=? and (u.user_name like "+c+" or u.user_id like "+c+")")
				.order("create_time desc").page(pageNum, 8, person_id);
		List<Record> records = page.getList();
	
		for (Record r : records) {
			r.set("CNsex", GeneralMethod.isSex(r.getInt("sex")));
			r.set("CNcreatetime", TimeUtil.me().stampToDateInYear(r.getLong("create_time").toString()));
			r.set("condition", condition[0]);
		}
		return page;
	}

	/**
	 * 添加患者就诊记录
	 * 
	 * @param pathogeny
	 * @return
	 */
	public boolean addinfo(String pathogeny, int user_id, int person_id) {
		long today = TimeUtil.me().getDayBeginTimestamp();
		long nextday = today + TimeUtil.me().ONE_DAY;
		if (SqlBuilder.from(RegisterRecord.table)
				.where("user_id=? and person_id=? and create_time>=? and create_time<?", user_id, person_id, today,
						nextday)
				.update("flag=0") == 1) {
			MedicalRecord mr = new MedicalRecord();
			mr.setPathogeny(pathogeny);
			mr.setPersonId(person_id);
			mr.setUserId(user_id);
			mr.setCreateTime(System.currentTimeMillis());
			try {
				return mr.save();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		} else {
			return false;
		}

	}

	/**
	 * 跟新记录
	 * 
	 * @param id
	 * @param path
	 * @return
	 */
	public int updateinfo(int id, String path) {
		long time = System.currentTimeMillis();
		return SqlBuilder.from(MedicalRecord.table).where("id=?", id).update("pathogeny=?,create_time=?", path, time);

	}

	public String isSex(int i) {
		String sex = "未知";
		switch (i) {
		case 1:
			sex = "男";
			break;
		case 2:
			sex = "女";
			break;
		case 3:
			sex = "未知";
			break;

		default:
			break;
		}
		return sex;
	}

	/**
	 * 通过openid获取用户病历信息
	 * 
	 * @param openid
	 * @return
	 */
	public List<Record> medicalInfo_get(String openid) {
		List<Record> Records = SqlBuilder.from(MedicalRecord.table).alias("m")
				.join(JOIN.LEFT, User.table, "u", "m.user_id=u.user_id").field("*")
				.join(JOIN.LEFT, HospitalPerson.table, "p", "p.person_id=m.person_id").where("openid=?").select(openid);
		for (Record r : Records) {
			System.out.println(r.getLong("create_time"));
			r.set("createtime", TimeUtil.me().stampToDateInYear(r.getLong("create_time").toString()));
			r.set("CNsex", isSex(r.getInt("sex")));
		}
		return Records;
	}
}
